"""
Case Type   : datakit安装
Case Name   : 【服务器管理】新增、编辑、删除标签，给物理机添加、移除标签
Create At   : 2025/5/20
Owner       : @lonely-dance
Description :
    1.创建标签
    2.编辑标签
    3.添加服务器
    4.服务器添加标签
    5.服务器删除标签
    6.删除标签
    7.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
    6.成功
    7.成功
History     :
"""

import os
import re
import unittest

from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.datakit_api.CommonApi import CommonApi
from testcase.utils.CommonDatakit import CommonDatakit


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.com_sh = CommonSH()
        self.pri_root_node = Node('PrimaryRoot')
        self.pri_node = Node('PrimaryDbUser')
        self.datakit_node = Node('Datakit_Root')
        self.token = self.com_sh.get_token_value(macro.DATAKIT_PASSWD)
        self.log.info(self.token)
        self.assertNotEqual("error", self.token)
        self.com_api = CommonApi(self.token)
        self.com_datakit = CommonDatakit(self.token)

    def test_base_ops(self):
        text = f'----step1:创建标签 expect：成功----'
        self.log.info(text)
        data = {"id": "", "name": "test"}
        response = self.com_api.post_hostTag_add(data)
        self.log.info(response.json())
        self.assertEqual(200, response.json()['code'], '执行失败' + text)

        text = f'----step2:编辑标签 expect:成功----'
        self.log.info(text)
        self.log.info('获取标签ID')
        response = self.com_api.get_hostTag_page(name='test')
        self.log.info(response.json())
        tag_id = response.json()['rows'][0]['id']
        data = {"name": "test1"}
        response = self.com_api.put_hostTag_update(tag_id=tag_id, data=data)
        self.log.info(response.json())
        self.assertEqual(200, response.json()['code'], '执行失败' + text)

        text = f'----step3:添加服务器 expect:成功----'
        self.log.info(text)
        self.com_datakit.import_host([self.pri_root_node])
        response = self.com_api.get_host_page(name=f"{self.pri_root_node.ssh_host}")
        self.log.info(response.json())
        host_id = response.json()['rows'][0]['hostId']

        text = '----step4:服务器添加标签 expect:成功'
        self.log.info(text)
        data = {"hostIds": [f"{host_id}"], "names": ["test1"]}
        response = self.com_api.put_hostTag_addTag(data=data)
        self.log.info(response.json())
        self.assertEqual(200, response.json()['code'], '执行失败' + text)

        text = '----step5:服务器删除标签 expect:成功'
        self.log.info(text)
        response = self.com_api.put_hostTag_delTag(data=data)
        self.log.info(response.json())
        self.assertEqual(200, response.json()['code'], '执行失败' + text)

        text = '----step6:删除标签 expect:成功'
        self.log.info(text)
        response = self.com_api.delete_hostTag_del(tag_id)
        self.log.info(response.json())
        self.assertEqual(200, response.json()['code'], '执行失败' + text)

    def tearDown(self):
        self.log.info('----step5:清理环境 expect:成功----')
        self.com_datakit.delete_host()
        self.log.info(f"-----{os.path.basename(__file__)} finsh-----")
